package com.party.web.biz.crowdfund;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.crowdfund.Analyze;
import com.party.core.model.crowdfund.ProjectAnalyze;
import com.party.core.model.crowdfund.ProjectLabel;
import com.party.core.model.crowdfund.SupportCount;
import com.party.core.model.label.Label;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.crowdfund.IAnalyzeService;
import com.party.core.service.crowdfund.IProjectLabelService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ISupportService;
import com.party.core.service.label.ILabelService;
import com.party.web.biz.file.ExcelExportService;
import com.party.web.biz.file.FileBizService;
import com.party.web.utils.excel.ExelClolerType;
import com.party.web.utils.excel.ExportExcel;
import com.party.web.web.dto.output.crowdfund.AnalyzeOutput;
import com.party.web.web.dto.output.crowdfund.TypeCountOutput;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 众筹分析业务接口
 * Created by wei.li
 *
 * @date 2017/7/10 0010
 * @time 15:04
 */
@SuppressWarnings("all")
@Service
public class AnalyzeBizService {

    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private IProjectService projectService;
    @Autowired
    private ISupportService supportService;
    @Autowired
    private IProjectLabelService projectLabelService;
    @Autowired
    private ILabelService labelService;
    @Autowired
    private ProjectBizService projectBizService;
    @Autowired
    private CosBizService cosBizService;
    @Autowired
    private ExcelExportService excelExportService;
    @Autowired
    private FileBizService fileBizService;
    @Autowired
    private IAnalyzeService analyzeService;

    public List<AnalyzeOutput> list(ProjectAnalyze projectAnalyze, Page page) {
        String currentGradationId = projectAnalyze.getCurrentGradationId();
        Integer isMultiple = StringUtils.isNotEmpty(currentGradationId) ? YesNoStatus.YES.getCode() : YesNoStatus.NO.getCode();
        List<ProjectAnalyze> list = projectService.analyzeList(projectAnalyze, page);
        List<AnalyzeOutput> analyzeOutputList = LangUtils.transform(list, new Function<ProjectAnalyze, AnalyzeOutput>() {
            @Override
            public AnalyzeOutput apply(ProjectAnalyze projectAnalyze) {
                projectAnalyze.setCurrentGradationId(currentGradationId);
                //统计总数
                AnalyzeOutput output = AnalyzeOutput.transform(projectAnalyze);

                convert(projectAnalyze, output);
                Date now = new Date();
                Date startDate = DateUtils.addDay(now, -15);
                Double sum = supportService.countAll(DateUtils.formatDate(startDate), projectAnalyze.getId());
                List<SupportCount> countList = supportService.count(DateUtils.formatDate(DateUtils.addDay(now, -15)), DateUtils.getTodayStr(), projectAnalyze.getId());

                Map<String, Double> moneyMap = Maps.newHashMap();
                List<Date> dateList = dateList();
                for (Date date : dateList) {
                    Float mapSum = 0f;
                    for (SupportCount supportCount : countList) {
                        if (DateUtils.formatDate(date, "yy/MM/dd").equals(DateUtils.formatDate(supportCount.getDate(), "yy/MM/dd"))) {
                            mapSum = supportCount.getPayment();
                        }
                    }

                    Double mapSum2 = BigDecimalUtils.add(mapSum, sum);
                    mapSum2 = BigDecimalUtils.round(mapSum2, 2);
                    moneyMap.put(DateUtils.formatDate(date, "yy/MM/dd"), mapSum2);
                    sum = mapSum2;
                }

                if (null != projectAnalyze.getRecentlyRecordDate()) {
                    Integer diffDay = DateUtils.diffDate(new Date(), projectAnalyze.getRecentlyRecordDate());
                    StringBuilder stringBuilder
                            = new StringBuilder(DateUtils.formatDate(projectAnalyze.getRecentlyRecordDate(), "yy/MM/dd"))
                            .append("--").append(diffDay).append("天前");
                    output.setRecentlyRecordTime(stringBuilder.toString());
                }

                output.setMoneyMap(moneyMap);

                if (projectAnalyze.getIsSuccess() == 0) {
                    int stagnationDays = 0;
                    if (projectAnalyze.getLastSupportDate() != null) {
                        //存在支持者
                        stagnationDays = DateUtils.diffDate(now, projectAnalyze.getLastSupportDate());
                    } else {
                        //不存在支持者从创建日开始算起
                        stagnationDays = DateUtils.diffDate(now, projectAnalyze.getCreateDate());
                    }
                    output.setStagnationDays(stagnationDays);
                }

                output.setUseTime(getUseTime(projectAnalyze.getUseTime()));

                //二维码
//                String codeUrl = projectBizService.getQrCodeUrl(projectAnalyze.getAuthorId(), projectAnalyze.getId());
//                String url = projectBizService.getUrl(projectAnalyze.getId());
//                output.setQrCodeUrl(codeUrl);
//                output.setUrl(url);

                String filePath = fileBizService.getUploadCIPath(projectAnalyze.getPayType() == 1 ? "directPay" : "crowdfund", projectAnalyze.getAuthorId());
                String qrCodeUrl = cosBizService.getPreviewPictureUrl(filePath + projectAnalyze.getId());
                String url = projectBizService.getUrl(projectAnalyze.getId(), isMultiple);
                output.setQrCodeUrl(qrCodeUrl);
                output.setUrl(url);
                return output;
            }
        });
        return analyzeOutputList;
    }


    /**
     * 统计类型数
     *
     * @param projectAnalyze 分析视图
     * @return 统计数
     */
    public TypeCountOutput countList(ProjectAnalyze projectAnalyze) {
        TypeCountOutput typeCountOutput = new TypeCountOutput();
        int all = 0;
        int underway = 0;
        int isSuccess = 0;
        int refunding = 0;
        int refunded = 0;
        if (!Strings.isNullOrEmpty(projectAnalyze.getTargetId())) {
            all = projectService.sizeForTargetId(projectAnalyze.getTargetId());
            underway = projectService.sizeForTargetId(projectAnalyze.getTargetId(), Constant.IS_CROWFUND_ING);
            isSuccess = projectService.sizeForTargetId(projectAnalyze.getTargetId(), Constant.IS_CROWFUND_SUCCESS);
            refunding = projectService.sizeForTargetId(projectAnalyze.getTargetId(), Constant.CROWDFUND_PROJECT_REFUNDING);
            refunded = projectService.sizeForTargetId(projectAnalyze.getTargetId(), Constant.CROWDFUND_PROJECT_REFUNDED);
        } else if (!Strings.isNullOrEmpty(projectAnalyze.getEventId())) {
            all = projectService.countForEvent(projectAnalyze.getEventId());
            underway = projectService.countForEvent(projectAnalyze.getEventId(), Constant.IS_CROWFUND_ING);
            isSuccess = projectService.countForEvent(projectAnalyze.getEventId(), Constant.IS_CROWFUND_SUCCESS);
            refunding = projectService.countForEvent(projectAnalyze.getEventId(), Constant.CROWDFUND_PROJECT_REFUNDING);
            refunded = projectService.countForEvent(projectAnalyze.getEventId(), Constant.CROWDFUND_PROJECT_REFUNDED);
        }
        typeCountOutput.setAll(all);
        typeCountOutput.setUnderway(underway);
        typeCountOutput.setSuccess(isSuccess);
        typeCountOutput.setRefunding(refunding);
        typeCountOutput.setRefunded(refunded);
        return typeCountOutput;
    }

    /**
     * 加载直接付款众筹类型统计列表
     *
     * @param projectAnalyze
     * @return
     */
    public TypeCountOutput countDirectList(ProjectAnalyze projectAnalyze) {
        TypeCountOutput typeCountOutput = new TypeCountOutput();
        int all = 0;
        int underway = 0;
        int isSuccess = 0;
        int refunding = 0;
        int refunded = 0;
        int refundFailed = 0;
        if (!Strings.isNullOrEmpty(projectAnalyze.getTargetId())) {
            all = projectService.directPaySizeForTargetId(projectAnalyze.getTargetId(), null, 1);
            underway = projectService.directPaySizeForTargetId(projectAnalyze.getTargetId(), Constant.IS_CROWFUND_ING, 1);
            isSuccess = projectService.directPaySizeForTargetId(projectAnalyze.getTargetId(), Constant.IS_CROWFUND_SUCCESS, 1);
            refunding
                    = projectService.directPaySizeForTargetId(projectAnalyze.getTargetId(),
                    Constant.CROWDFUND_PROJECT_REFUNDING, 1);
            refunded
                    = projectService.directPaySizeForTargetId(projectAnalyze.getTargetId(),
                    Constant.CROWDFUND_PROJECT_REFUNDED, 1);
            refundFailed = projectService.directPaySizeForTargetId(projectAnalyze.getTargetId(),
                    5, 1);//退款失败
        } else if (!Strings.isNullOrEmpty(projectAnalyze.getEventId())) {
            all = projectService.dpCountForEvent(projectAnalyze.getEventId(), null);
            underway = projectService.dpCountForEvent(projectAnalyze.getEventId(), Constant.IS_CROWFUND_ING);
            isSuccess = projectService.dpCountForEvent(projectAnalyze.getEventId(), Constant.IS_CROWFUND_SUCCESS);
            refunding = projectService.dpCountForEvent(projectAnalyze.getEventId(), Constant.CROWDFUND_PROJECT_REFUNDING);
            refunded = projectService.dpCountForEvent(projectAnalyze.getEventId(), Constant.CROWDFUND_PROJECT_REFUNDED);
            refundFailed = projectService.dpCountForEvent(projectAnalyze.getEventId(),
                    5);//退款失败
        }
        typeCountOutput.setAll(all);
        typeCountOutput.setUnderway(underway);
        typeCountOutput.setSuccess(isSuccess);
        typeCountOutput.setRefunding(refunding);
        typeCountOutput.setRefunded(refunded);
        typeCountOutput.setRefundFailed(refundFailed);
        return typeCountOutput;
    }


    /**
     * 获取时间列表
     *
     * @return 时间列表
     */
    public List<Date> dateList() {
        Date now = new Date();
        Date startDate = DateUtils.addDay(now, -14);
        List<Date> dateList = DateUtils.getDateDifference(startDate, now);
        return dateList;
    }


    public List<String> dateStringList() {
        List<Date> list = dateList();
        List<String> dateList = Lists.newArrayList();
        for (Date date : list) {
            dateList.add(DateUtils.formatDate(date, "yy/MM/dd"));
        }
        Collections.reverse(dateList);
        return dateList;
    }

    /**
     * 转换输出视图
     *
     * @param projectAnalyze 项目分析
     * @param output         输出视图
     */
    public void convert(ProjectAnalyze projectAnalyze, AnalyzeOutput output) {
        if (null != projectAnalyze.getCompetitionStatus()) {
            if (YesNoStatus.YES.getCode().equals(projectAnalyze.getCompetitionStatus())) {
                output.setCompetitionStatus("参与");
            } else if (YesNoStatus.NO.getCode().equals(projectAnalyze.getCompetitionStatus())) {
                output.setCompetitionStatus("未参与");
            }
        }
        if (null != projectAnalyze.getIsFriend()) {
            output.setIsFriend(YesNoStatus.getValue(projectAnalyze.getIsFriend()));
        }
        if (null != projectAnalyze.getIsGroup()) {
            output.setIsGroup(YesNoStatus.getValue(projectAnalyze.getIsGroup()));
        }
        if (null != projectAnalyze.getIsSubscribe()) {
            output.setIsSubscribe(YesNoStatus.getValue(projectAnalyze.getIsSubscribe()));
        }
        if (null != projectAnalyze.getIsPoster()) {
            output.setIsPoster(YesNoStatus.getValue(projectAnalyze.getIsPoster()));
        }
        if (null != projectAnalyze.getIsSuccess()) {
            if (projectAnalyze.getIsSuccess().equals(0)) {
                output.setIsSuccess(projectAnalyze.getPayType() == 1 ? "未支付" : "众筹中");
            }
            if (projectAnalyze.getIsSuccess().equals(1)) {
                output.setIsSuccess(projectAnalyze.getPayType() == 1 ? "支付成功" : "众筹成功");
            }
            if (projectAnalyze.getIsSuccess().equals(2)) {
                output.setIsSuccess("众筹失败");
            }
            if (projectAnalyze.getIsSuccess().equals(3)) {
                output.setIsSuccess("退款中");
            }
            if (projectAnalyze.getIsSuccess().equals(4)) {
                output.setIsSuccess("退款成功");
            }
            if (projectAnalyze.getIsSuccess().equals(5)) {
                output.setIsSuccess("退款失败");
            }
        }
        if (StringUtils.isNotEmpty(projectAnalyze.getCurrentGradationId()) && null != projectAnalyze.getCompetitionStatus()) {
            if (YesNoStatus.YES.getCode().equals(projectAnalyze.getCompetitionStatus())) {
                output.setCompetitionStatus("已参与");
            } else {
                output.setCompetitionStatus("未参与");
            }
        }
        if (StringUtils.isNotEmpty(projectAnalyze.getCurrentGradationId())) {
            Double remindAmount = BigDecimalUtils.sub(output.getActualAmount(), output.getAllConsumedAmount());
            output.setRemindAmount(BigDecimalUtils.round(remindAmount, 2));
        }
        if (!CollectionUtils.isEmpty(projectAnalyze.getLabelList())) {
            List<String> labelNames = LangUtils.transform(projectAnalyze.getLabelList(), new Function<Label, String>() {
                @Override
                public String apply(Label label) {
                    return StringUtils.isEmpty(label.getName()) ? "" : label.getName();
                }
            });
            String lables = Joiner.on(",").join(labelNames);
            output.setLabels(lables);
            output.setStyle(projectAnalyze.getLabelList().get(0).getStyle());
            output.setLabelId(projectAnalyze.getLabelList().get(0).getId());
        }
    }


    /**
     * 保存标签
     *
     * @param id        标签编号
     * @param projectId 项目编号
     */
    @Transactional
    public String labelSave(String id, String projectId) {
        //删除项目关系
        projectLabelService.deleteByProjectId(projectId);
        ProjectLabel projectLabel = new ProjectLabel();
        projectLabel.setProjectId(projectId);
        projectLabel.setLabelId(id);
        projectLabelService.insert(projectLabel);
        Label label = labelService.get(id);
        return label.getStyle();
    }

    /**
     * 导出excel
     *
     * @param fileName 文件名称
     * @param list     分析列表
     * @param response 响应参数
     * @throws IOException
     */
    public String export(String fileName, List<AnalyzeOutput> list, HttpServletResponse response, Integer isMultiple) throws IOException {
        List<String> headerList = Lists.newArrayList();
        if (YesNoStatus.NO.getCode().equals(isMultiple)) {
            headerList
                    = Lists.newArrayList("众筹者", "渠道商", "联合发起人", "参与状态", "跟进记录", "最近跟进记录", "区域", "公司", "职务", "联系电话",
                    "加好友", "做海报", "入群状态", "关注", "状态", "支持人数", "支持金额", "阅读数", "二维码", "报名时间", "众筹停滞", "众筹用时");
        } else {
            headerList
                    = Lists.newArrayList("众筹者", "渠道商", "联合发起人", "参与状态", "跟进记录", "最近跟进记录", "区域", "公司", "职务", "联系电话",
                    "加好友", "做海报", "入群状态", "关注", "支持人数", "支持金额", "活动金额", "参赛状态", "消费金额", "剩余金额", "阅读数", "二维码", "报名时间", "众筹停滞", "众筹用时");
        }
        List<String> dateList = dateStringList();
        for (String title : dateList) {
            headerList.add(title);
        }

        ExportExcel exportExcel = new ExportExcel(fileName, headerList);
        exportExcel.setWb(new SXSSFWorkbook(500));
        exportExcel.setSheet(exportExcel.getWb().createSheet("Export"));
        exportExcel.setStyles(exportExcel.createStyles(exportExcel.getWb()));
        exportExcel.initialize(fileName, headerList);
        if (YesNoStatus.NO.getCode().equals(isMultiple)) {
            exportExcel = setDataList(list, exportExcel);
        } else {
            exportExcel = setDataListMulti(list, exportExcel);
        }
        String allPath = excelExportService.getExcelPath(fileName, "analyze", exportExcel);
        return allPath;
    }

    /**
     * 导出直接付款众筹 excel
     *
     * @param fileName 文件名称
     * @param list     分析列表
     * @param response 响应参数
     * @throws IOException
     */
    public String exportDirectPay(String fileName, List<AnalyzeOutput> list,
                                  HttpServletResponse response) throws IOException {
        List<String> headerList
                = Lists.newArrayList("众筹者", "渠道商", "区域", "公司", "职务", "联系电话",
                "加好友", "做海报", "入群状态", "关注", "状态", "支付金额", "二维码", "支付时间");
        ExportExcel exportExcel = new ExportExcel(fileName, headerList);
        exportExcel.setWb(new SXSSFWorkbook(500));
        exportExcel.setSheet(exportExcel.getWb().createSheet("Export"));
        exportExcel.setStyles(exportExcel.createStyles(exportExcel.getWb()));
        exportExcel.initialize(fileName, headerList);
        exportExcel = setDpDataList(list, exportExcel);
        String allPath = excelExportService.getExcelPath(fileName, "analyze", exportExcel);
        return allPath;
    }


    /**
     * 设置导出内容
     *
     * @param list        输出列表
     * @param exportExcel 导出excel
     * @return 导出excel
     */
    public ExportExcel setDataList(List<AnalyzeOutput> list, ExportExcel exportExcel) {
        List<String> dateList = dateStringList();
        for (AnalyzeOutput analyzeOutput : list) {
            Row row = exportExcel.addRow();

            // row样式
            CellStyle cellStyle = exportExcel.getWb().createCellStyle();
            //单元格背景设置
            if (null != analyzeOutput.getStyle()
                    && !analyzeOutput.getStyle().equals(ExelClolerType.WHITE.getValue())) {
                short index = ExelClolerType.getIndex(analyzeOutput.getStyle());
                if (index != 0) {
                    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                    cellStyle.setFillForegroundColor(index);
                }
            }

            exportExcel.addCell(row, 0, analyzeOutput.getAuthorName(), cellStyle);
            exportExcel.addCell(row, 1, analyzeOutput.getPublisher(), cellStyle);
            exportExcel.addCell(row, 2, analyzeOutput.getParentName(), cellStyle);
            exportExcel.addCell(row, 3, analyzeOutput.getLabels(), cellStyle);
            exportExcel.addCell(row, 4, analyzeOutput.getRecentlyRecord(), cellStyle);
            exportExcel.addCell(row, 5, analyzeOutput.getRecentlyRecordTime(), cellStyle);
            exportExcel.addCell(row, 6, analyzeOutput.getCityName(), cellStyle);
            exportExcel.addCell(row, 7, analyzeOutput.getCompany(), cellStyle);
            exportExcel.addCell(row, 8, analyzeOutput.getJobTitle(), cellStyle);
            exportExcel.addCell(row, 9, analyzeOutput.getMobile(), cellStyle);
            exportExcel.addCell(row, 10, analyzeOutput.getIsFriend(), cellStyle);
            exportExcel.addCell(row, 11, analyzeOutput.getIsPoster(), cellStyle);
            exportExcel.addCell(row, 12, analyzeOutput.getIsGroup(), cellStyle);
            exportExcel.addCell(row, 13, analyzeOutput.getIsSubscribe(), cellStyle);
            exportExcel.addCell(row, 14, analyzeOutput.getIsSuccess(), cellStyle);
            exportExcel.addCell(row, 15, analyzeOutput.getFavorerNum(), cellStyle);
            exportExcel.addCell(row, 16, analyzeOutput.getActualAmount(), cellStyle);
            exportExcel.addCell(row, 17, analyzeOutput.getViewNum(), cellStyle);
            exportExcel.addCell(row, 18, analyzeOutput.getQrCodeUrl(), cellStyle);
            exportExcel.addCell(row, 19, analyzeOutput.getCreateDate(), cellStyle);
            exportExcel.addCell(row, 20, analyzeOutput.getStagnationDays(), cellStyle);
            exportExcel.addCell(row, 21, analyzeOutput.getUseTime(), cellStyle);
            for (int i = 0; i < dateList.size(); i++) {
                String title = dateList.get(i);
                exportExcel.addCell(row, 22 + i, analyzeOutput.getMoneyMap().get(title), cellStyle);
            }
        }
        return exportExcel;
    }


    /**
     * 设置导出内容
     *
     * @param list        输出列表
     * @param exportExcel 导出excel
     * @return 导出excel
     */
    public ExportExcel setDataListMulti(List<AnalyzeOutput> list, ExportExcel exportExcel) {
        List<String> dateList = dateStringList();
        for (AnalyzeOutput analyzeOutput : list) {
            Row row = exportExcel.addRow();

            // row样式
            CellStyle cellStyle = exportExcel.getWb().createCellStyle();
            if (null != analyzeOutput.getStyle()
                    && !analyzeOutput.getStyle().equals(ExelClolerType.WHITE.getValue())) {
                short index = ExelClolerType.getIndex(analyzeOutput.getStyle());
                if (index != 0) {
                    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                    cellStyle.setFillForegroundColor(index);
                }
            }
            exportExcel.addCell(row, 0, analyzeOutput.getAuthorName(), cellStyle);
            exportExcel.addCell(row, 1, analyzeOutput.getPublisher(), cellStyle);
            exportExcel.addCell(row, 2, analyzeOutput.getParentName(), cellStyle);
            exportExcel.addCell(row, 3, analyzeOutput.getLabels(), cellStyle);
            exportExcel.addCell(row, 4, analyzeOutput.getRecentlyRecord(), cellStyle);
            exportExcel.addCell(row, 5, analyzeOutput.getRecentlyRecordTime(), cellStyle);
            exportExcel.addCell(row, 6, analyzeOutput.getCityName(), cellStyle);
            exportExcel.addCell(row, 7, analyzeOutput.getCompany(), cellStyle);
            exportExcel.addCell(row, 8, analyzeOutput.getJobTitle(), cellStyle);
            exportExcel.addCell(row, 9, analyzeOutput.getMobile(), cellStyle);
            exportExcel.addCell(row, 10, analyzeOutput.getIsFriend(), cellStyle);
            exportExcel.addCell(row, 11, analyzeOutput.getIsPoster(), cellStyle);
            exportExcel.addCell(row, 12, analyzeOutput.getIsGroup(), cellStyle);
            exportExcel.addCell(row, 13, analyzeOutput.getIsSubscribe(), cellStyle);
            exportExcel.addCell(row, 14, analyzeOutput.getFavorerNum(), cellStyle);
            exportExcel.addCell(row, 15, analyzeOutput.getActualAmount(), cellStyle);

            exportExcel.addCell(row, 16, analyzeOutput.getGradationTargetAmount(), cellStyle);
            exportExcel.addCell(row, 17, analyzeOutput.getCompetitionStatus(), cellStyle);
            exportExcel.addCell(row, 18, analyzeOutput.getConsumedAmount(), cellStyle);
            exportExcel.addCell(row, 19, analyzeOutput.getRemindAmount(), cellStyle);

            exportExcel.addCell(row, 20, analyzeOutput.getViewNum(), cellStyle);
            exportExcel.addCell(row, 21, analyzeOutput.getQrCodeUrl(), cellStyle);
            exportExcel.addCell(row, 22, analyzeOutput.getCreateDate(), cellStyle);
            exportExcel.addCell(row, 23, analyzeOutput.getStagnationDays(), cellStyle);
            exportExcel.addCell(row, 24, analyzeOutput.getUseTime(), cellStyle);
            for (int i = 0; i < dateList.size(); i++) {
                String title = dateList.get(i);
                exportExcel.addCell(row, 25 + i, analyzeOutput.getMoneyMap().get(title), cellStyle);
            }
        }
        return exportExcel;
    }


    /**
     * 设置直接付款列表导出内容
     *
     * @param list        输出列表
     * @param exportExcel 导出excel
     * @return 导出excel
     */
    public ExportExcel setDpDataList(List<AnalyzeOutput> list, ExportExcel exportExcel) {
        List<String> dateList = dateStringList();
        for (AnalyzeOutput analyzeOutput : list) {
            Row row = exportExcel.addRow();

            // row样式
            CellStyle cellStyle = exportExcel.getWb().createCellStyle();
            //单元格背景设置
            if (null != analyzeOutput.getStyle()
                    && !analyzeOutput.getStyle().equals(ExelClolerType.WHITE.getValue())) {
                short index = ExelClolerType.getIndex(analyzeOutput.getStyle());
                if (index != 0) {
                    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                    cellStyle.setFillForegroundColor(index);
                }
            }
            exportExcel.addCell(row, 0, analyzeOutput.getAuthorName(), cellStyle);
            exportExcel.addCell(row, 1, analyzeOutput.getPublisher(), cellStyle);
            exportExcel.addCell(row, 2, analyzeOutput.getCityName(), cellStyle);
            exportExcel.addCell(row, 3, analyzeOutput.getCompany(), cellStyle);
            exportExcel.addCell(row, 4, analyzeOutput.getJobTitle(), cellStyle);
            exportExcel.addCell(row, 5, analyzeOutput.getMobile(), cellStyle);
            exportExcel.addCell(row, 6, analyzeOutput.getIsFriend(), cellStyle);
            exportExcel.addCell(row, 7, analyzeOutput.getIsPoster(), cellStyle);
            exportExcel.addCell(row, 8, analyzeOutput.getIsGroup(), cellStyle);
            exportExcel.addCell(row, 9, analyzeOutput.getIsSubscribe(), cellStyle);
            exportExcel.addCell(row, 10, analyzeOutput.getIsSuccess(), cellStyle);
            exportExcel.addCell(row, 11, !analyzeOutput.getIsSuccess().equals("未支付") ? analyzeOutput.getActualAmount() : "0.00", cellStyle);
            exportExcel.addCell(row, 12, analyzeOutput.getQrCodeUrl(), cellStyle);
            exportExcel.addCell(row, 13, !analyzeOutput.getIsSuccess().equals("未支付") ? analyzeOutput.getCreateDate() : "", cellStyle);
        }
        return exportExcel;
    }


    /**
     * 格式化使用时间
     *
     * @param diffTime
     * @return
     */
    public String getUseTime(Long diffTime) {
/*        if (diffTime == null || diffTime == 0) {
            return null;
        }
        long day = diffTime / (24 * 60 * 60 * 1000);
        String useTime = "";
        if (day > 0) {
            useTime += day + "天";
        } else {
            useTime = "1天";
        }*/
        return DateUtils.getDateDiff(diffTime, "hours");
    }

    public void updateSubscribe(String openId, int isSubscribe) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<String> projectIds = projectService.findByOpenId(openId);
                for (String projectId : projectIds) {
                    try {
                        Analyze analyze = analyzeService.findByTargetId(projectId);
                        if (null != analyze) {
                            analyze.setIsSubscribe(isSubscribe);
                            analyzeService.update(analyze);
                        }
                    } catch (Exception e) {
                        logger.error("更新众筹分析的关注状态异常，projectId:" + projectId, e);
                        continue;
                    }
                }
            }
        }).start();
    }
}
